//
//  ParserBRITE.java
//  TopoManipApplet
//
//  Created by Thibaud PIQUET on 23/03/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

package src.convertisseur;
import src.convertisseur.Resume;
import java.util.*;
import java.io.*;
import org.jdom.*;

/**
 *
 * @author Thibaud PIQUET
 * Version 1.1 - Ksoobrayen : Evolutions vers un fichier graphML --> Le 04 mars 2011
 */
public class ParserBRITE implements Runnable {
	
    File ftp; // File to parse
	File fileOut; // File to create
    Reader fr; // File Reader
    BufferedReader br; // Buffer Reader
    String line;
    String chaine;
    String[] res;

    String[] keyGraphNode = {"name","status","AS","switch","nodetype","location","indegree","outdegree"};
    String[] keyGraphEdge = {"srcAS","tgtAS","status","bandwidth","delay","length","linkType"};
	
    public ParserBRITE(File file, File fileOUT) throws IOException {
        this.ftp = file;
		this.fileOut = fileOUT;
        this.fr = new FileReader(this.ftp);
        this.br = new BufferedReader(this.fr);
    }
	
	public void run() {
		try {
			this.parseToXML();
		}
		catch (Exception e) {
			e.printStackTrace();
			Resume result = Resume.getInstance();
			result.add("ParserBRITE","procedure run had some problems : "+e);
		}
	}
	
	
    public void parseToXML() throws IOException {

        // <graphml xmlns="http://graphml.graphdrawing.org/xmlns">
        // <graph edgedefault="undirected">

        Element graphml = new Element("graphml");
        Namespace nsGraphml = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns");
        graphml.setNamespace(nsGraphml);


        Element graph = new Element("graph");
        Attribute edgedefault = new Attribute("edgedefault", "undirected");
        graph.setAttribute(edgedefault);

	
		
	Element domain = new Element("domain");
	Element information = new Element("information");
        Element topology = new Element("topology");
	Element igp = new Element("igp");
	Element nodes = new Element("nodes");
	Element links = new Element("links");
	Element igpLinks = new Element("links");
        
        org.jdom.Document document = new Document(graphml);

		Resume result = Resume.getInstance();

		try {
			line = this.br.readLine();
			
			Element title = new Element("title");
			Element date = new Element("date");
			Element author = new Element("author");
			Element description = new Element("description");
			Date today = new Date();
			String day = "";
			String month = "";
			
			title.setText(fileOut.getName());
			if(today.getDate()<10) { day = "0"+today.getDate();} else { day = today.getDate()+""; }
			if((today.getMonth()+1)<10){ month = "0"+(today.getMonth()+1);} else { month = (today.getMonth()+1)+"";}
			date.setText((today.getYear()+1900)+"-"+month+"-"+day);
			author.setText("File Generator");
			description.setText("File generated by ParserBRITE from this jApplet");
			
			information.addContent(title);
			information.addContent(date);
			information.addContent(author);
			information.addContent(description);
			
			String state = "";
						
			while (line!=null){
				if (line!=null){
					if(line.startsWith("Toppology")) { /*System.out.println(line);*/}
					else if(line.startsWith("Model")) { /*System.out.println(line);*/}
					else if(line.startsWith("Nodes:")) {
						state="Node";
						//System.out.println(line);
					}
					else if(line.startsWith("Edges:")) {
						state="Edges";
						//System.out.println(line);
					}
					else if(line.startsWith(" ")) {
						// do nothing
					}
					else{
						if(state.equals("Node")) {
							res = line.split(" ");
							// node id	res[0]
							// node as      res[5]
							// node type    res[6]
							// node location x  res[1]
							// node location y  res[2]
							// node indegree    res[3]
							// node outdegree   res[4]
							
							/*
							 for(int i=0;i<res.length;i++) {
							 System.out.println("res["+i+"] : "+res[i]);
							 }
							 */
							
							if(!(res.length<7)) {
								Element node = new Element("node");
								Attribute id = new Attribute("id",res[0]);

                                                                Element nomNode = Utils.getNameNode("name", res[0]);
								
								Element statusNode = Utils.getNameNode("status", "UP");
                                                                Element asNode = Utils.getNameNode("AS", res[5]);
                                                                Element switchNode = Utils.getNameNode("switch", "false");

								Element nodeType = Utils.getNameNode("nodetype", "");
                                                                Attribute type1 = new Attribute("type1",res[6]);
                                                                node.addContent(nodeType);
								
								Element location = Utils.getNameNode("location", "");
								Attribute xCoordonate = new Attribute("x",res[1]);
								Attribute yCoordonate = new Attribute("y",res[2]);
								location.setAttribute(xCoordonate);
								location.setAttribute(yCoordonate);

								Element indegree = Utils.getNameNode("indegree", res[3]);
        							Element outdegree = Utils.getNameNode("outdegree", res[4]);

                                                                node.setAttribute(id);
                                                                node.addContent(nomNode);
								node.addContent(statusNode);
								node.addContent(asNode);
								node.addContent(switchNode);
								nodeType.setAttribute(type1);
								
								node.addContent(location);
								node.addContent(indegree);
								node.addContent(outdegree);
								nodes.addContent(node);
							}                   
						}
						else if(state.equals("Edges")) {
							res = line.split(" ");
							// link id	res[0]
							// source id    res[1]
							// source as    res[6]
							// target id    res[2]
							// target as    res[7]
							// length       res[3]
							// delay        res[4]
							// bandwidth    res[5]
							// type         res[8]
							
							/*
							 for(int i=0;i<res.length;i++) {
							 System.out.println("Res["+i+"] : "+res[i]);
							 }
							 */
							
							if(!(res.length<9)) {
								
								Element link = new Element("edge");
								Attribute idLink = new Attribute("id",res[0]);
								Attribute srcNode = new Attribute("source",res[1]);
                                                                Attribute tgtNode = new Attribute("target",res[2]);

								//Element source = new Element("source");
								Element  srcAS = Utils.getNameNode("srcAS", res[6]);
                                                                //Attribute srcAS = new Attribute("AS",res[6]);
								
								//Element target = new Element("target");
								Element  tgtAS = Utils.getNameNode("tgtAS", res[6]);
								//Attribute tgtAS = new Attribute("AS",res[7]);
								
								Element statusLink = Utils.getNameNode("status", "UP");

								Element bandwidth = Utils.getNameNode("bandwidth", res[5]);

								Element delay = Utils.getNameNode("delay", res[4]);

								Element length =Utils.getNameNode("length", res[3]);

								Element linkType = Utils.getNameNode("linkType", "");
                                                                Attribute type3 = new Attribute("type3",res[8]);
                                                                linkType.setAttribute(type3);
								
								link.setAttribute(srcNode);
								link.setAttribute(tgtNode);
								link.setAttribute(idLink);
								link.addContent(srcAS);
								link.addContent(tgtAS);
								link.addContent(statusLink);
								link.addContent(bandwidth);
								link.addContent(delay);
								link.addContent(linkType);
								link.addContent(length);
								
								links.addContent(link);
							}
						}
					}
				}
				line = this.br.readLine();
			}
			br.close();
			fr.close();

                        for (int i = 0; i < keyGraphNode.length; i++) {
                            topology.addContent(Utils.getKeyGraph(i, keyGraphNode, "node"));

                        }

                        for (int i = 0; i < keyGraphEdge.length; i++) {
                            topology.addContent(Utils.getKeyGraph(i, keyGraphEdge, "edge"));

                        }
			
			topology.addContent(nodes);
			topology.addContent(links);
			
			domain.addContent(information);
			domain.addContent(topology);

                        graph.addContent(domain);
                        graphml.addContent(graph);
			
			org.jdom.output.XMLOutputter sortie = new org.jdom.output.XMLOutputter(org.jdom.output.Format.getPrettyFormat());
			sortie.output(document, new FileOutputStream(fileOut.getAbsolutePath()));
			
			result.add("ParserBRITE","Import succeeded");
			
		} catch (Exception e) {
			e.printStackTrace();
			result.add("ParserBRITE","Import had problems "+e);
		}
	}
}
